/*
 * This file is part of NativeAccessHooks.
 *
 * Copyleft 2014 Mark Jeronimus. All Rights Reversed.
 * https://code.google.com/p/nativeaccesshooks/
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with NativeAccessHooks. If not, see <http://www.gnu.org/licenses/>.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.digitalmodular.nah.keyboard;

import com.digitalmodular.nah.NativeHook;
import com.digitalmodular.nah.NativeInputEvent;

/**
 * The native event generated when a key has been pressed or released. The event is passed to every {@link NativeKeyListener} object
 * which registered to receive these events.
 * 
 * The {@link #getScanCode()} value translates to a physical key, and same values will always map to the same key location (save for
 * physical layout changes). For more details, see the {@link ScanCode} enum.
 * 
 * The {@link #getVirtualKey()} value translates to a symbol on a keycap, and same values will always map to the same key cap symbol
 * (save for some changes in the shift symbol). For more details, see the {@link VirtualKey} enum.
 * 
 * When the keyboard layout is changed, the same physical keys will still generate the same scanCode, but only keys which
 * (approximately) generate the same typed character will generate the same virtualKey.
 * 
 * @author Mark Jeronimus
 * @see ScanCode
 * @see VirtualKey
 * @since 1.0
 */
// date 2014-08-23
public class NativeKeyEvent extends NativeInputEvent {
	/** The first number in the range of ID's used for native key events. */
	public static final int	NATIVE_KEY_FIRST	= 2400;
	/** The last number in the range of ID's used for native key events. */
	public static final int	NATIVE_KEY_LAST		= 2402;

	public static enum NativeKeyEventType {
		/** The "native key typed" event ID. */
		Pressed,
		/** The "native key pressed" event ID. */
		Released;
	}

	public static enum ModifierKeys {
		/** The left shift key modifier constant. */
		LeftShift,
		/** The right shift key modifier constant. */
		RightShift,
		/** The left ctrl key modifier constant. */
		LeftCtrl,
		/** The right ctrl key modifier constant. */
		RightCtrl,
		/** The left alt key modifier constant. */
		LeftAltj,
		/** The right alt key modifier constant. */
		RightAlt,
		/** The left meta key modifier constant. */
		LeftWin,
		/** The right meta key modifier constant. */
		RightWin;
	};

	/** This is used to indicate that the keyChar is undefined. */
	public static final char			CHAR_UNDEFINED	= 0xFFFF;

	/** The kind of keyboard event (<i>e.g.</i> Pressed or Typed). */
	private final NativeKeyEventType	eventType;
	/** The scan code. */
	private final ScanCode				scanCode;
	/** The virtual key. */
	private final VirtualKey			virtualKey;

	/**
	 * Instantiates a new {@link NativeKeyEvent} object.
	 * 
	 * @param source The source of the event.
	 * @param time The time the event occurred.
	 * @param namoTime The nanoTime value of the virtual machine at the time the event occurred.
	 * @param eventType The kind of keyboard event (<i>e.g.</i> Pressed or Typed).
	 * @param scanCode The scan code.
	 * @param virtualKey The virtual key.
	 */
	public NativeKeyEvent(final NativeHook source, final long time, final long namoTime, final NativeKeyEventType eventType,
			final ScanCode scanCode, final VirtualKey virtualKey) {
		super(source, time, namoTime);
		this.eventType = eventType;
		this.scanCode = scanCode;
		this.virtualKey = virtualKey;
	}

	public NativeKeyEventType getEventType() {
		return eventType;
	}

	/**
	 * Returns the scan code.
	 * 
	 * @return
	 */
	public ScanCode getScanCode() {
		return scanCode;
	}

	/**
	 * Returns the virtual key.
	 * 
	 * @return
	 */
	public VirtualKey getVirtualKey() {
		return virtualKey;
	}

	@Override
	public String toString() {
		StringBuilder message = new StringBuilder(getClass().getSimpleName()).append("[");
		message.append(super.toString());
		message.append(", eventType=").append(eventType);
		message.append(", scanCode=").append(scanCode);
		message.append(", virtualKey=").append(virtualKey);
		return message.toString();
	}
}
